/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Business;

import Objects.Category;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nguyen Thanh Ha
 */
public class CategoryBusiness {

    public static ArrayList<Category> getAllCategory() throws SQLException {
        ArrayList<Category> list = new ArrayList<>();

        String sql = "{call sp_getAllCategory}";

        try {
            ResultSet rs = DBAccess.DB_Connect.executeQuery(sql, null);
            while (rs.next()) {
                Category cate = new Category();
                cate.setCategoryID(rs.getString("CategoryID"));
                cate.setCategoryName(rs.getString("CategoryName"));

                list.add(cate);
            }

        } catch (SQLException ex) {
            Logger.getLogger(CategoryBusiness.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CategoryBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

   

    public static ArrayList<Category> getCategoryByID(String id) {
        ArrayList<Category> list = new ArrayList<>();

        String sql = "{call sp_getCategoryByID(?)}";
        ArrayList list1 = new ArrayList();
        list1.add(id);

        ResultSet rs;
        try {
            rs = DBAccess.DB_Connect.executeQuery(sql, list1);
            while (rs.next()) {
                Category cate = new Category();
                cate.setCategoryID(rs.getString("CategoryID"));
                cate.setCategoryName(rs.getString("CategoryName"));          
                list.add(cate);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CategoryBusiness.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(CategoryBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }

        return list;
    }
    
     public static ArrayList<Category> getCategoryByName(String name) {
        ArrayList<Category> list = new ArrayList<>();

        String sql = "{call sp_getCategoryByName(?)}";
        ArrayList list1 = new ArrayList();
        list1.add(name);

        ResultSet rs;
        try {
            rs = DBAccess.DB_Connect.executeQuery(sql, list1);
            while (rs.next()) {
                Category cate = new Category();
                cate.setCategoryID(rs.getString("CategoryID"));
                cate.setCategoryName(rs.getString("CategoryName"));           
                list.add(cate);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CategoryBusiness.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(CategoryBusiness.class.getName()).log(Level.SEVERE, null, ex);
        }

        return list;
    }
}
